// Copyright  2004-2008 Backplane Ltd.
//
// This file is part of Ubiquity formsPlayer
//
// Ubiquity formsPlayer is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3 of the License, or (at your
// option) any later version.
//
// Ubiquity formsPlayer is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
// more details.
//
// You should have received a copy of the GNU General Public License
// along with Ubiquity formsPlayer If not, see <http://www.gnu.org/licenses/>.
//	
//
//   Purpose: Implementation of CFormControl methods that correspond to IBindingTarget.
//   $Id$



#include "stdafx.h"
#include "XForms.h"
#include "XFormsApp.h"

#include "XFormsElement.h"
 
const long g_lStateValid	 = 1l;
const long g_lStateEnabled	= 1l << 1;
const long g_lStateReadOnly = 1l << 2;
const long g_lStateRequired = 1l << 3;

//valid on/off
HRESULT CFormControl::oninvalid()
{
	//run custom events.
	return SignalEvent(g_lStateValid,false);
}

HRESULT CFormControl::onvalid()
{
	//run custom events.
	return SignalEvent(g_lStateValid,true);
}

//readonly on/off
HRESULT CFormControl::onreadonly()
{
	//run custom events.
	return SignalEvent(g_lStateReadOnly,true);
}

HRESULT CFormControl::onreadwrite()
{
	//run custom events.
	return SignalEvent(g_lStateReadOnly,false);
}

//required on/off

HRESULT CFormControl::onrequired()
{
	//run custom events.
	return SignalEvent(g_lStateRequired,true);
}

HRESULT CFormControl::onoptional()
{
	//run custom events.
	return SignalEvent(g_lStateRequired,false);
}

//relevant on/off
HRESULT CFormControl::onenabled()
{
	//run custom events.
	return SignalEvent(g_lStateEnabled,true);
}

HRESULT CFormControl::ondisabled()
{
	//run custom events.
	return SignalEvent(g_lStateEnabled,false);
}


HRESULT CFormControl::onvaluechanged()
{
	HRESULT hr = S_OK;
	m_bValueChanged = true;
	m_bStateChanged = true;
	return hr;
}

HRESULT CFormControl::setInitalState(bool bEnabled, bool bReadOnly, bool bValid, bool bRequired)
{
	HRESULT hr = S_OK;

	if(m_pXFormsElement)
		hr = m_pXFormsElement->SetClassProperty(bEnabled, bReadOnly, bValid, bRequired);

	return hr;
}

void CFormControl::bindingToNode(bool bBinding)
{
	if(m_pXFormsElement) 
		m_pXFormsElement->setBindingToTarget(bBinding);
}

void CFormControl::FireStateEvents()
{
	if(m_bStateChanged)
	{
		m_bStateChanged = false;

		if(m_lState & g_lStateValid)
		{
			FireEvent(EVENT_VALID);
		}
		else
		{
			FireEvent(EVENT_INVALID);
		}
		if(m_lState & g_lStateEnabled)
		{
			FireEvent(EVENT_ENABLED);
		}
		else
		{
			FireEvent(EVENT_DISABLED);
		}
		if(m_lState & g_lStateReadOnly)
		{
			FireEvent(EVENT_READONLY);
		}
		else
		{
			FireEvent(EVENT_READWRITE);
		}
		if(m_lState & g_lStateRequired)
		{
			FireEvent(EVENT_REQUIRED);
		}
		else
		{
			FireEvent(EVENT_OPTIONAL);
		}
			
		if(m_bValueChanged)
		{
			FireEvent(EVENT_VALUECHANGED);
			m_bValueChanged = false;
		}
	}
	
}

HRESULT CFormControl::SignalEvent(long lState,bool bValue)
{
	HRESULT hr;
	if(bValue)
	{
		if(m_lState & lState)
		{
			hr = S_FALSE;
		}
		else
		{
			m_lState |= lState;
			hr = S_OK;
			m_bStateChanged = true;
		}

	}
	else
	{
		if(m_lState & lState)
		{
			m_lState ^= lState;
			hr = S_OK;
			m_bStateChanged = true;
		}
		else
		{
			hr = S_FALSE;
		}
	}
	return S_FALSE;
}

//TODO: the above methods should be removed,  and replaced with
//	the code below.  To do this would require changing
//	the point at which controls register with bound nodes 
//	it also requires changing the type passed to various methods,
//	and the point in the inheritance tree at which IBindingTarget
//	is inherited from.
//	Also, moving the method declarations from CFormControl
//	to CImplNodeBinding.

/*
 
//valid on/off
HRESULT CImplNodeBinding::oninvalid()
{

	//run custom events.
	return SignalEvent(EVENT_INVALID);
}

HRESULT CImplNodeBinding::onvalid()
{
	//run custom events.
	return SignalEvent(EVENT_VALID);
}

//readonly on/off
HRESULT CImplNodeBinding::onreadonly()
{
	//run custom events.
	return SignalEvent(EVENT_READONLY);
}

HRESULT CImplNodeBinding::onreadwrite()
{
	//run custom events.
	return SignalEvent(EVENT_READWRITE);
}

//required on/off

HRESULT CImplNodeBinding::onrequired()
{
	//run custom events.
	return SignalEvent(EVENT_REQUIRED);
}

HRESULT CImplNodeBinding::onoptional()
{
	//run custom events.
	return SignalEvent(EVENT_OPTIONAL);
}

//relevant on/off
HRESULT CImplNodeBinding::onenabled()
{
	//run custom events.
	return SignalEvent(EVENT_ENABLED);
}

HRESULT CImplNodeBinding::ondisabled()
{
	//run custom events.
	return SignalEvent(EVENT_DISABLED);
}


HRESULT CImplNodeBinding::onvaluechanged()
{
	//run custom events.
	hr = SignalEvent(EVENT_VALUECHANGED);
	return hr;
}
*/